const db = require("../db");

exports.getArticleCates = (req, res) => {
  const sql = "select * from ev_article_cate where is_delete=0 and Id=?";
  db.query(sql, req.params.id, (err, results) => {
    if (err) return res.cc(err);
    console.log(results);

    // 因为不能返回标记为删除的数据，所以我们查询的时候请注意输入的id是未标记删除的，否则会返回空数组，得到以下报错
    if (results.length !== 1) return res.cc("根据id获取文章失败");
    res.send({
      status: 0,
      message: "获取成功",
      data: results,
    });
  });
};

exports.addArticleCates = (req, res) => {
  const sql = "insert into ev_article_cate set ? ";
  db.query(sql, req.body, (err, results) => {
    if (err) return res.cc(err);
    if (results.affectedRows !== 1) return res.cc("添加文章类别失败");

    res.cc("added article successfully", 0);
  });
};

exports.deleteCateById = (req, res) => {
  const sql = "update ev_article_cate set is_delete=1 where id = ?";
  db.query(sql, req.params.id, (err, results) => {
    if (err) return res.cc(err);
    if (results.affectedRows !== 1) return res.cc("删除文章分类失败");
    res.cc("removed successfully", 0);
  });
};

exports.updateCateById = (req, res) => {
  const sql = "update ev_article_cate set ? where id = ? ";
  db.query(sql, [req.body, req.body.id], (err, results) => {
    if (err) return res.cc(err);
    if (results.affectedRows !== 1) return res.send("更新失败");
    res.cc("updated category successfully", 0);
  });
};
